<?php
namespace Admin\Models\res;

use Phalcon\Di;
use Helpers\CurlClient as HttpClient;
use Admin\Models\InterfaceService;

/**
 * Class Dictionaries 字典
 * @package Admin\Models\Question
 */
class QuestionPrivilege extends InterfaceService
{
    private static $interfaceKey = 'questionPrivilegeUrl';
    const GET_BOOK_SCOPE_TYPE = '/api/v1/userBookPowers/scopeType';//查询书本公开范围的所有用户信息
    const BOOK_ACTION_POWERS = '/api/v1/userBookPowers';//设置用户基本权限
    const GET_USER_BOOK_POWERS = '/api/v1/users/{userId}/userBookPowers';//获取用户的书本权限
    const GET_USER_BOOK_POWERS_DETAIL = '/api/v1/userBookPowers/bookInfo';//获取用户的书本权限_详细数据
    const DELETE_USER_BOOK_POWERS = '/api/v1/userBookPowers/{userBookPowerId}';//删除人员书本权限
    const BOOK_SCHOOL_SCOPES = '/api/v1/bookSchoolScopes';//根据书本id查询学校idList

    /**
     * #方法说明：题库数据层
     * # Created by wy
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     *
     * @param $httpUrl
     * @param array $paramArr
     *
     * @return bool|string
     *
     * created by wy
     */
    public static function createHttpUrl($httpUrl, $paramArr = array())
    {
        return parent::createInterfaceUrl($httpUrl, $paramArr, self::$interfaceKey);
    }

    /**
     * #方法说明：查询书本公开范围的人员
     * @param array $paramArr
     * @param array $urlArr
     * @return array|bool|mixed|string
    # Created by wy
     */
    public function getBookScopeUser($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_BOOK_SCOPE_TYPE, $urlArr));
        $result = $http->get(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], [200, 201])) {
            return $http->bodyConvert();
        } else {
            return array();
        }
    }

    /**
     * #方法说明：设置人员权限：新接口
     * @param array $paramArr
     * @param array $urlArr
     * @return array|bool|mixed|string
    # Created by wy
     */
    public function setUserBookPowers($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::BOOK_ACTION_POWERS, $urlArr));
        $result = $http->json(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], [200, 201])) {
            return array(
                'msg' => '操作成功',
                'code' => 200
            );
        } else {
            return $http->bodyConvert();
        }
    }

    /**
     * #方法说明：获取人员的书本权限
     * @param array $paramArr
     * @param array $urlArr
     * @return array|bool|mixed|string
    # Created by wy
     */

    public function getUserBookPowers($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_USER_BOOK_POWERS, $urlArr));
        $result = $http->get(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], [200, 201])) {
            return $http->bodyConvert();
        } else {
            return array();
        }
    }

    /**
     * 根据书本id获取用户数据
     *
     * @param array $paramArr
     * @param array $urlArr
     *
     * @return array|bool|mixed|string
     *
     * create by qgz 20180828
     */
    public function getUserBookPowersDetail($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_USER_BOOK_POWERS_DETAIL, $urlArr));
        $result = $http->get(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], [200, 201])) {
            return $http->bodyConvert();
        } else {
            return array();
        }
    }

    /**
     * #方法说明：删除人员书本权限
     * @param array $paramArr
     * @param array $urlArr
     * @return array|bool|mixed|string
    # Created by wy
     */
    public function deleteUserBookPowers($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::DELETE_USER_BOOK_POWERS, $urlArr));
        $result = $http->delete(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], [200, 201, 204])) {
            return array(
                'msg' => '操作成功',
                'code' => 200
            );
        } else {
            return $http->bodyConvert();
        }
    }

    /**
     * 根据书id获取学校id列表
     *
     * @param array $paramArr
     * @param array $urlArr
     *
     * @return array|bool|mixed|string
     *
     * create by qgz 20180827
     */
    public function getBookSchoolScopes($paramArr = [], $urlArr = [])
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::BOOK_SCHOOL_SCOPES, $urlArr));
        $result = $http->get(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], [200, 201])) {
            return $http->bodyConvert();
        } else {
            return [];
        }
    }

    /**
     * 查询书本哪些应用使用
     *
     * @param string $bookId
     *
     * @return array|bool|mixed|string
     *
     * create by qgz 20181113
     */
    public function getBookAppById($bookId = '')
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/bookAppScopes/packageNameList', ['bookId' => $bookId]));
        $result = $http->get()->headerConvert();
        if (in_array($result['statusCode'], [200, 201])) {
            return $http->bodyConvert();
        } else {
            return [];
        }
    }
}